/** @noSelfInFile */
import {UIEventList} from '../../event'
import {KEYBORD_KEY} from '@kernel/constant/keyboard'
import Cache from '@kernel/util/cache'

type UICallback = (params: Event['TRIGGER_COMPONENT_EVENT']) => void;


const cache = new Cache();

export default class EventUtil {
    static index = 0;

    static register<Key extends keyof Event>(eventKey: Key, callBack: (data: Event[Key]) => void) {
        EventUtil.index++;
        let id = 10000 + EventUtil.index;

        let calls: UICallback[] = []
        if (! cache.has(eventKey)) {
            let trigger = new_global_trigger(id, '事件_' + id, EVENT[eventKey] as string, true);
            cache.set(eventKey, calls)
            trigger.on_event = (trigger: string, event: any, data: any) => {
                calls.forEach(call => {
                    call(data)
                })
            }
        } else {
            calls = cache.get(eventKey)
        }

        calls.push(callBack)
    }

    /**
     * 注册ui事件.
     * 注意：需要在GAME_INIT前调用
     * @param eventKey
     * @param event
     */
    static register2<Key extends keyof Event>(eventKey: Key, event?: string) {
        EventUtil.index++;
        let id = 10000 + EventUtil.index;
        const eventName = event ? [EVENT[eventKey] as string, event] : EVENT[eventKey]

        let trigger = new_global_trigger(id, '事件_' + id, eventName, true);
        (trigger.on_event as any) = (trigger: string, event: any, actor, data: any) => {
            const calls = cache.get<UICallback[]>(eventKey, () => [])
            if (calls) {
                calls.forEach(call => {
                    call(data)
                })
            }
        }
    }
}

type EventName = keyof Event

const _event: EventName[] = [
    'LOADING_END',
    'UNIT_ATTR_CHANGE',
    'UNIT_ATTR_CHANGE',
    'UNIT_BORN',
    'UNIT_DIE',
    'REVIVE_UNIT',
    'UNIT_REMOVE',
    'KILL_UNIT',
    'UNIT_ADD_ITEM',
    'UNIT_REMOVE_ITEM',
    'UNIT_USE_ITEM',
    'SELECT_UNIT',
    'SELECT_UNIT_GROUP',
    'UNIT_BE_HURT',
    'UNIT_HURT_OTHER',
    'UNIT_ON_COMMAND',
    'UPGRADE_UNIT',
    'UNIT_END_NAV_EVENT',
    'UNIT_START_NAV_EVENT',
    'UNIT_EXIT_BATTLE',
    'UNIT_ON_ADD_EXP',
    'AREA_ENTER',
    'AREA_LEAVE',
    'ABILITY_BUILD_FINISH',
    'ROLE_RESOURCE_CHANGED',
    'ROLE_TECH_CHANGED',
    'START_SKILL_POINTER',
    'STOP_SKILL_POINTER',
    'CHAT_SEND_GM',
    'SELECT_ITEM',
    'ABILITY_PLUS_POINT',
    'ABILITY_LOSE',
    'ABILITY_ATTR_CHANGED',
    'ABILITY_CS_START',
    'ABILITY_CS_END',
    'ABILITY_PS_START',
    'ABILITY_PS_END',
    'ABILITY_CST_END',
    'ABILITY_END',
    'MODIFIER_CYCLE_TRIGGER',
    'SELECT_DEST',
]
// 注册ui事件转发至TRIGGER_COMPONENT_EVENT
UIEventList.forEach(event => {
    EventUtil.register2('TRIGGER_COMPONENT_EVENT', event)
})
// 注册硬件事件转发KEYBOARD_KEY_UP_EVENT
for (let key in KEYBORD_KEY) {
    const code = KEYBORD_KEY[key]
    if (code) {
        EventUtil.register2('KEYBOARD_KEY_UP_EVENT', code)
    }
}
// 注册硬件事件转发KEYBOARD_KEY_DOWN_EVENT
for (let key in KEYBORD_KEY) {
    const code = KEYBORD_KEY[key]
    if (code) {
        EventUtil.register2('KEYBOARD_KEY_DOWN_EVENT', code)
    }
}
// 注册初始事件
_event.forEach(evt => {
    EventUtil.register2(evt)
})
